package com.jay.compass;

import java.io.InputStream;

import android.content.ContentUris;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

public class FragmentAddress extends Fragment {

	private Button button;
	private final int CONTACT_PICKER_RESULT = 0x88;
	private ImageView imageView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.fragment_address, container,
				false);
		button = (Button) rootView.findViewById(R.id.address);
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Intent.ACTION_PICK,
						ContactsContract.Contacts.CONTENT_URI);
				getActivity().startActivityForResult(intent,
						CONTACT_PICKER_RESULT);
			}
		});

		imageView = (ImageView) rootView.findViewById(R.id.imageView1);
		return rootView;
	}

	public void setImage(long id) {
		Uri uri = ContentUris.withAppendedId(
				ContactsContract.Contacts.CONTENT_URI, id);
		InputStream input = ContactsContract.Contacts
				.openContactPhotoInputStream(
						getActivity().getContentResolver(), uri);
		Bitmap bmp_head = BitmapFactory.decodeStream(input);
		imageView.setImageBitmap(bmp_head);
	}

}
